#include<iostream>
#include<pthread.h>
#include<string>

using namespace std;

pthread_mutex_t _mutex;
int ticket=10000;

void* Rob(void*name)
{
    string s=(const char*)name;

    while(true)
    {
        pthread_mutex_lock(&_mutex);
        if(ticket>0)
        {
            cout<<s<<": "<<ticket--<<endl;
        }
        else
        {
            pthread_mutex_unlock(&_mutex);
            break;
        }
        pthread_mutex_unlock(&_mutex);
    }

    return nullptr;
}


int main()
{
    pthread_t t1=0;
    pthread_t t2=0;
    pthread_t t3=0;
    pthread_t t4=0;

    pthread_mutex_init(&_mutex,nullptr);

    pthread_create(&t1,nullptr,Rob,(void*)"thread1");
    pthread_create(&t2,nullptr,Rob,(void*)"thread2");
    pthread_create(&t3,nullptr,Rob,(void*)"thread3");
    pthread_create(&t4,nullptr,Rob,(void*)"thread4");

    cout<<"11111111111111111111"<<endl;
    pthread_join(t1,nullptr);
    
    pthread_join(t2,nullptr);
    pthread_join(t3,nullptr);
    pthread_join(t4,nullptr);
    cout<<"22222222222222222222222222222"<<endl;
    return 0;
}